package net.maku.module.organization.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.common.utils.TreeUtils;
import net.maku.module.business.service.DictionariesSubsetService;
import net.maku.module.organization.convert.OrganizationConvert;
import net.maku.module.organization.vo.OrganStaffVO;
import net.maku.module.staff.entity.StaffEntity;
import net.maku.module.staff.service.StaffService;
import net.maku.newbase.*;
import net.maku.module.organization.entity.OrganizationEntity;
import net.maku.module.organization.service.OrganizationService;
import net.maku.newbase.util.redis.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织
 *
 * @author
 */
@RestController
@RequestMapping("nb/organization")
@Tag(name = "组织")
@AllArgsConstructor
public class OrganizationController extends NewBaseController<OrganizationService, OrganizationEntity> {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private DictionariesSubsetService dictionariesSubsetService;

    @Autowired
    private StaffService staffService;

    /**
     * 分页查询
     *
     * @param page
     * @param query
     * @return
     */
    @Operation(summary = "分页")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result page(PageParam page, NewQuery query) {
        LambdaQueryWrapper<OrganizationEntity> select = new LambdaQueryWrapper<>();
        select.eq(OrganizationEntity::getDeleted, 0);

        IPage<OrganizationEntity> iPage = entityService.page(page.b(), select);
        return Result.ok(new PageResult<>(OrganizationConvert.INSTANCE.convertList(iPage.getRecords()), iPage.getTotal()));
    }

    /**
     * 查询 列表
     *
     * @return
     */
    @Operation(summary = "分页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list() {
        List<OrganizationEntity> list = entityService.list();
    /*    JSONArray array = dictionariesSubsetService.findByDictId(1688546635935301630L);
        Map<Long, String> map = dictionariesSubsetService.dict(array);

        for (int i = 0; i < list.size(); i++) {
            list.get(i).setTypeName(map.get(list.get(i).getTypeId()));
        }*/
        return Result.ok(TreeUtils.build(OrganizationConvert.INSTANCE.convertList(list), Constant.ROOT));
    }

    /**
     * 查询 组织子集
     *
     * @return
     */
    @Operation(summary = "分页")
    @RequestMapping(value = "/findChildList", method = RequestMethod.GET)
    public R findChildList(Long id) {
        List<OrganizationEntity> orgList = entityService.findChildList(id);
        List<StaffEntity> staffList = staffService.findListByOrgId(id);

        List<OrganStaffVO> orgVO = new ArrayList<>();
        orgList.forEach(e->{
            orgVO.add(new OrganStaffVO(e.getName(),e.getId(),1));
        });
        staffList.forEach(e->{
            orgVO.add(new OrganStaffVO(e.getName(),e.getId(),2));
        });

        return R.ok(orgVO);
    }

}